3.4.áRunning a virtual machine

The "Start" button in the main window starts the virtual machine that is currently selected.

This opens up a new window, and the virtual machine which you selected will boot up. Everything which would normally be seen on the virtual system's monitor is shown in the window, as can be seen with the image in Sectioná1.1, “Virtualization basics”.

In general, you can use the virtual machine much like you would use a real computer. There are couple of points worth mentioning however.

3.4.1.áKeyboard and mouse support in virtual machines

3.4.1.1.áCapturing and releasing keyboard and mouse

Since the operating system in the virtual machine does not "know" that it is not running on a real computer, it expects to have exclusive control over your keyboard and mouse. This is, however, not the case since, unless you are running the VM in full-screen mode, your VM needs to share keyboard and mouse with other applications and possibly other VMs on your host.

As a result, initially after installing a host operating system and before you install the guest additions (we will explain this in a minute), only one of the two -- your VM or the rest of your computer -- can "own" the keyboard and the mouse. You will see a second mouse pointer which will always be confined to the limits of the VM window. Basically, you activate the VM by clicking inside it.

To return ownership of keyboard and mouse to your host operating system, VirtualBox reserves a special key on your keyboard for itself: the "host key". By default, this is the right Control key on your keyboard; on a Mac host, the default host key is the left Command key. You can change this default in the VirtualBox Global Settings. In any case, the current setting for the host key is always displayed at the bottom right of your VM window, should you have forgotten about it:

In detail, all this translates into the following:

  • Your keyboard is owned by the VM if the VM window on your host desktop has the keyboard focus (and then, if you have many windows open in your guest operating system as well, the window that has the focus in your VM). This means that if you want to type within your VM, click on the title bar of your VM window first.

    To release keyboard ownership, press the Host key (as explained above, typically the right Control key).

    Note that while the VM owns the keyboard, some key sequences (like Alt-Tab for example) will no longer be seen by the host, but will go to the guest instead. After you press the host key to reenable the host keyboard, all key presses will go through the host again, so that sequences like Alt-Tab will no longer reach the guest.

  • Your mouse is owned by the VM only after you have clicked in the VM window. The host mouse pointer will disappear, and your mouse will drive the guest's pointer instead of your normal mouse pointer.

    Note that mouse ownership is independent of that of the keyboard: even after you have clicked on a titlebar to be able to type into the VM window, your mouse is not necessarily owned by the VM yet.

    To release ownership of your mouse by the VM, also press the Host key.

As this behavior can be inconvenient, VirtualBox provides a set of tools and device drivers for guest systems called the "VirtualBox Guest Additions" which make VM keyboard and mouse operation a lot more seamless. Most importantly, the Additions will get rid of the second "guest" mouse pointer and make your host mouse pointer work directly in the guest.

This will be described later in Chapterá4, The VirtualBox Guest Additions.

3.4.1.2.áTyping special characters

Operating systems expect certain key combinations to initiate certain procedures. Some of these key combinations may be difficult to enter into a virtual machine, as there are three candidates as to who receives keyboard input: the host operating system, VirtualBox, or the guest operating system. Who of these three receives keypresses depends on a number of factors, including the key itself.

  • Host operating systems reserve certain key combinations for themselves. For example, it is impossible to enter the Ctrl+Alt+Delete combination if you want to reboot the guest operating system in your virtual machine, because this key combination is usually hard-wired into the host OS (both Windows and Linux intercept this), and pressing this key combination will therefore reboot your host.

    Also, with Linux, the key combination Ctrl+Alt+Backspace normally resets the X server (to restart the entire graphical user interface in case it got stuck). As the X server intercepts this combination, pressing it will usually restart your host graphical user interface (and kill all running programs, including VirtualBox, in the process).

    Third, also with Linux, the key combination Ctrl+Alt+Fx (where Fx is one of the function keys from F1 to F12) normally allows to switch between virtual terminals. As with Ctrl+Alt+Delete, these combinations are intercepted by the host operating system and therefore always switch terminals on the host.

    If, instead, you want to send these key combinations to the guest operating system in the virtual machine, you will need to use one of the following methods:

    • Use the items in the "Machine" menu of the virtual machine window. There you will find "Insert Ctrl+Alt+Delete" and "Ctrl+Alt+Backspace"; the latter will only have an effect with Linux guests, however.

    • Press special key combinations with the Host key (normally the right Control key), which VirtualBox will then translate for the virtual machine:

      • Host key + Del to send Ctrl+Alt+Del (to reboot the guest);

      • Host key + Backspace to send Ctrl+Alt+Backspace (to restart the graphical user interface of a Linux guest);

      • Host key + F1 (or other function keys) to simulate Ctrl+Alt+F1 (or other function keys, i.e. to switch between virtual terminals in a Linux guest).

  • For some other keyboard combinations such as Alt-Tab (to switch between open windows), VirtualBox allows you to configure whether these combinations will affect the host or the guest, if a virtual machine currently has the focus. This is a global setting for all virtual machines and can be found under "File" -> "Global settings" -> "Input" -> "Auto-capture keyboard".

3.4.2.áChanging removable media

While a virtual machine is running, you can change removable media from the "Devices" menu of the VM's window. Here you can select in detail what VirtualBox presents to your VM as a CD, DVD, or floppy.

The settings are the same as would be available for the VM in the "Settings" dialog of the VirtualBox main window, but since that dialog is disabled while the VM is in "running" or "saved" state, this extra menu saves you from having to shut down and restart the VM every time you want to change media.

Hence, in the "Devices" menu, VirtualBox allows you to attach the host drive to the guest or select a floppy or DVD image using the Disk Image Manager, all as described in Sectioná3.3, “Basics of virtual machine configuration”.

3.4.3.áSaving the state of the machine

When you click on the "Close" button of your virtual machine window (at the top right of the window, just like you would close any other window on your system) (or press the Host key together with "Q"), VirtualBox asks you whether you want to "save" or "power off" the VM.

The difference between these two options is crucial. They mean:

  • Save the machine state: With this option, VirtualBox "freezes" the virtual machine by completely saving its state to your local disk. When you later resume the VM (by again clicking the "Start" button in the VirtualBox main window), you will find that the VM continues exactly where it was left off. All your programs will still be open, and your computer resumes operation.

    Saving the state of a virtual machine is thus in some ways similar to suspending a laptop computer (e.g. by closing its lid).

  • Send the shutdown signal. This will send an ACPI shutdown signal to the virtual machine, which has the same effect as if you had pressed the power button on a real computer. So long as a a fairly modern operating system is installed and running in the VM, this should trigger a proper shutdown mechanism in the VM.

  • Power off the machine: With this option, VirtualBox also stops running the virtual machine, but without saving its state.

    This is equivalent of pulling the power plug on a real computer without shutting it down properly. If you start the machine again after powering it off, your operating system will have to reboot completely and may begin a lengthy check of its (virtual) system disks.

    As a result, this should not normally be done, since it can potentially cause data loss or an inconsistent state of the guest system on disk.

The "Discard" button in the main VirtualBox window discards a virtual machine's saved state. This has the same effect as powering it off, and the same warnings apply.

3.4.4.áSnapshots

With VirtualBox's snapshots, you can save a particular state of a virtual machine for later use. At any later time, you can revert to that state, even though you may have changed the VM considerably since then.

This is particularly useful for making sure that a guest installation is not damaged by accidental changes, misbehaving software, or viruses.

Once you have set up the machine the way you want it, simply take a snapshot, and should anything happen to the installation, you can simply revert to its snapshot state.

To take a snapshot of your VM, perform the following steps:

  1. If your VM is currently in either the "saved" or the "powered off" state (as displayed next to the VM in the VirtualBox main window), click on the "Snapshots" tab on the top right of the main window, and then on the small camera icon (for "Take snapshot").

    If your VM is currently running, select "Take snapshot" from the "Machine" pull-down menu of the VM window.

  2. A window will pop up and ask you to name the snapshot. This name is purely for reference purposes to help you remember the state of the snapshot. For example, a useful name would be "Fresh installation from scratch, no external drivers".

  3. Your new snapshot will then appear in the list of snapshots under the "Snapshots" tab. Underneath, you will see an item called "Current state", signifying that the current state of your VM is a variation based on the snapshot you took earlier.

    (If you later take another snapshot, you will see that they will be displayed in sequence, and each subsequent snapshot is a derivation of the earlier one.)

To revert to an earlier snapshot, you click on the "Current state" item and select "Discard current state". This will bring the VM back to the state of the nearest (most recent) snapshot. In the same way, you can merge several earlier snapshots into one.

Note

The snapshot reverted to will affect the virtual hard drives that are connected to your VM, as the entire state of the virtual hard drive will be reverted as well. This means also that all files that have been created since the snapshot and all other file changes will be lost. In order to prevent such data loss while still making use of the snapshot feature, it is possible to add a second hard drive in "write-through" mode using the VBoxManage interface and use it to store your data. As write-through hard drives are not included in snapshots, they remain unaltered when a machine is reverted. See Chapterá5, Virtual storage for details.